package cn.tedu.ivos.geofence.service.impl;

import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.base.response.StatusCode;
import cn.tedu.ivos.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import cn.tedu.ivos.geofence.pojo.entity.Geofence;
import cn.tedu.ivos.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos.geofence.service.GeofenceService;
import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏业务:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //之前没有给围栏设置车辆相关的数据,所以现在需要为每个围栏补全车辆数据
        //1.遍历上方查到的围栏集合
        for (int i = 0; i < list.size(); i++) {
            //2.取出当前遍历到的围栏对象
            GeofenceVO geofenceVO = list.get(i);
            //3.准备车辆查询对象
            VehicleQuery vehicleQuery = new VehicleQuery();
            //4.向车辆查询对象中设置车辆查询条件:当前围栏的id
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            //5.调用车辆模块方法,查询车辆
            List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
            //6.给围栏设置绑定车辆总数
            int totalNum = vehicleVOList.size();
            //7.定义变量用来保存围栏上绑定的可用车辆数
            int availableNum = 0;
            //8.依次遍历车辆列表中的每一辆车,如果车辆的状态为空闲,可用车辆数+1
            for (VehicleVO vehicleVO : vehicleVOList) {
                if(vehicleVO.getStatus().equals("1")){
                    availableNum++;
                }
            }
            //9.为围栏补全数据
            geofenceVO.setTotalNum(totalNum);
            geofenceVO.setAvailableNum(availableNum);
            geofenceVO.setVehicleList(vehicleVOList);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        log.debug("保存电子围栏业务:geofenceSaveParam={}", geofenceSaveParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceSaveParam, geofence);
        log.debug("中间数据:geofence={}", geofence);
        if (geofence.getId() != null) {//修改
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        } else {//新增
            geofence.setStatus("1");//启用状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏业务:id={}", id);
        //1.封装车辆的查询条件
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        //2.调用车辆模块的功能查询当前围栏上绑定的车辆数据
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //3.如果该围栏下有车辆,业务层需要抛出异常
        if (list.size() > 0 && list != null) {
            //throw new ServiceException(StatusCode.OPERATION_FAILED);
            /*抛出更加明确的异常,但注意需要在StatusCode中新增一个值*/
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        } else {
            //4.如果该围栏下没有车辆,正常删除
            geofenceMapper.deleteGeofence(id);
        }
    }
}
